//
//  EvaluateCalculatorViewController.m
//  Rong360
//
//  Created by tianliwei on 8/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "EvaluateCalculatorViewController.h"
#import "EvaluCalCell.h"
#import "EvaluCalculator.h"
#import "EvaluateResultViewController.h"
#import "EvalueHistoryViewController.h"
@interface EvaluateCalculatorViewController ()
@property (nonatomic, strong) CalculatePanelView *calculateView;
//@property (nonatomic, strong) UIButton *hsPrevBtn;
//@property (nonatomic, strong) NSArray *hsDataSource;
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *subTitles;
@property (nonatomic, strong) UITextField *currentField;
@property (nonatomic, strong) EvaluCalculator *evaluCal;
@property (nonatomic, assign) double sfzb;//首付准备
@property (nonatomic, assign) double mygfzc;//每月最高购房支出
@property (nonatomic, assign) double gjjkded;//公积金可贷额度
@property (nonatomic, assign) double sdqx;//商贷期限
@property (nonatomic, assign) BOOL bBegin;
@property (nonatomic, copy) NSString *housingType;

@end

static NSString *CellIdentifier = @"Cell";

@implementation EvaluateCalculatorViewController

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.housingType = @"普通住宅";
    self.evaluCal = [[EvaluCalculator alloc]init];
    self.title = @"购房能力评估器";
    self.titles = @[@"首付最多可提供",@"首付最少可提供",@"每月最高购房支出",@"公积金可贷额度",@"商贷期限"];
    self.subTitles = @[@"万",@"万",@"元",@"万",@"年"];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, (UI_NAVIFRAME_HEIGHT - CalculatorViewH))];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = (UI_NAVIFRAME_HEIGHT - CalculatorViewH)/self.titles.count;
    self.tableView.scrollEnabled = YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"EvaluCalCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
 
    self.calculateView = [[[NSBundle mainBundle]loadNibNamed:@"CalculatePanelView" owner:self options:NULL]lastObject];
    self.calculateView.delegate = self;
    self.calculateView.Y = UI_NAVIFRAME_HEIGHT - CalculatorViewH;
    [self.view addSubview:self.calculateView];
}

- (void)history{
    self.hidesBottomBarWhenPushed = YES;
    EvalueHistoryViewController *evalueHistoryVC = [[EvalueHistoryViewController alloc]init];
    [self.navigationController pushViewController:evalueHistoryVC animated:YES];
}

- (void)calculate{
    switch (self.currentField.tag) {
        case 0:
            self.evaluCal.cmax = self.currentField.text;
            break;
        case 1:
            self.evaluCal.cmin = self.currentField.text;
            break;
        case 2:
            self.evaluCal.cmon_max = self.currentField.text;
            break;
        case 3:
            self.evaluCal.gloan = self.currentField.text;
            break;
        case 4:
            self.evaluCal.deadline = self.currentField.text;
            break;
        default:
            break;
    }
    if (0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"对不起，请将选项填写完整" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }else{
        [self.evaluCal beginCalculate];
        self.hidesBottomBarWhenPushed = YES;
        EvaluateResultViewController *evaluateResultVC = [[EvaluateResultViewController alloc]initWithMinHouse:self.evaluCal.cmin_house maxHouse:self.evaluCal.cmax_house];
        [self.navigationController pushViewController:evaluateResultVC animated:YES];
    }
}

- (void)keyBtnClicked:(UIButton *)sender{
    NSString *currentStr = self.currentField.text;
    switch (sender.tag) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
            [self inputNumber:sender.titleLabel.text];
            break;
        case 10:{//"."
            BOOL notDot = ([self.currentField.text rangeOfString:@"."].location == NSNotFound);
            if(notDot){
                currentStr= [currentStr stringByAppendingString:@"."];
                self.currentField.text= currentStr;
            }
            break;
        }
        case 11:{//"C"
            self.currentField.text = @"";
            self.bBegin = YES;
            break;
        }
            
        case 12:
            [self calculate];
            break;
        default:
            break;
    }
}

- (void)inputNumber:(NSString *)str
{
    
    if([self.currentField.text isEqualToString:@"0"] && [str isEqualToString:@"0"]){
        return;
    }
    if(self.bBegin){
        self.currentField.text= str;
    }else{
        self.currentField.text= [self.currentField.text stringByAppendingString:str];
    }
    self.bBegin= NO;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EvaluCalCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.mainLabel.text = self.titles[indexPath.row];
    cell.unitLabel.text = self.subTitles[indexPath.row];
    cell.textField.delegate = self;
    cell.textField.inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
    cell.textField.tag = indexPath.row;
    [cell.textField setUserInteractionEnabled:NO];
    //cell.textField.selected = NO;光标不闪，不可复制粘贴，但可以接受输入
    return cell;
}


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    switch (textField.tag) {
        case 0:
            self.evaluCal.cmax = textField.text;
            break;
        case 1:
            self.evaluCal.cmin = textField.text;
            break;
        case 2:
            self.evaluCal.cmon_max = textField.text;
            break;
        case 3:
            self.evaluCal.gloan = textField.text;
            break;
        case 4:
            self.evaluCal.deadline = textField.text;
            break;
        default:
            break;
    }
    [textField setUserInteractionEnabled:NO];
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    self.currentField = textField;
    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EvaluCalCell *cell = (EvaluCalCell *)[tableView cellForRowAtIndexPath:indexPath];
    [cell.textField setUserInteractionEnabled:YES];
    [cell.textField becomeFirstResponder];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */

@end
